package com.crm.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.crm.pojo.Contact;
import com.crm.pojo.Contactnote;
import com.crm.pojo.User;
import com.crm.service.ContactService;
import com.crm.service.ContactnoteService;
import com.crm.service.UserService;
import com.crm.util.DateUtil;

public class ContactnoteAction {

	private Contactnote contactnote;
	private String contactid;
	private String fid;
	
	private ContactnoteService contactnoteService;
	private ContactService contactService;
	private UserService userService;
	
	public String del()	{
		//System.out.println(contactnote.getId());
		if(fid == null) {
			List<Contactnote> noteList = contactnoteService.FindByFid(contactnote.getId());
			for(Contactnote note : noteList) {
				contactnoteService.del(note);
			}
		}
		
		Contactnote fatherNote = contactnoteService.get(contactnote.getId());
		contactnoteService.del(fatherNote);
		
		return null;
	}
	
	public String add() {
		if(contactnote.getContent() != null && !"".equals(contactnote.getContent())) {
			HttpServletRequest request = ServletActionContext.getRequest();
			
			User user = (User) request.getSession().getAttribute("user");
			User u = userService.get(user.getId());
			
			Contact contact = contactService.get(contactid);
			
			contactnote.setCreatetime(DateUtil.getNow());
			contactnote.setContact(contact);
			contactnote.setUser(u);
			if(fid != null) {
				//contactnote.setFid(fid);
				//System.out.println(fid);
				contactnote.setFid(fid);
			}
			
			contactnoteService.save(contactnote);
			//System.out.println(contactnote.getId());
			return "success";
		}
		return null;
	}
	
	
	//get set
	
	public void setContactnoteService(ContactnoteService contactnoteService) {
		this.contactnoteService = contactnoteService;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Contactnote getContactnote() {
		return contactnote;
	}

	public void setContactnote(Contactnote contactnote) {
		this.contactnote = contactnote;
	}

	public String getContactid() {
		return contactid;
	}

	public void setContactid(String contactid) {
		this.contactid = contactid;
	}
	public String getFid() {
		return fid;
	}
	public void setFid(String fid) {
		this.fid = fid;
	}
	
}
